# SPDX-FileCopyrightText: 2021 John Scott <jscott@posteo.net>
# SPDX-License-Identifier: GPL-3.0-or-later

project('which', 'c', license: 'GPL-3.0-or-later')
cc = meson.get_compiler('c')

if (not cc.has_header_symbol('limits.h', 'NL_TEXTMAX', args: '-D_POSIX_C_SOURCE=200809L')) and cc.has_header_symbol('assert.h', '__GLIBC__', required: true)
	add_project_arguments('-DNL_TEXTMAX=1024', language: 'c')
elif meson.can_run_host_binaries()
	result = cc.run('''
		#define _POSIX_C_SOURCE 200809L
		#include <limits.h>
		#include <string.h>
		int main(void) {
			char str[NL_TEXTMAX];
			memset(str, 0, sizeof(str));
		}
		''', name: 'automatic allocation of char[NL_TEXTMAX]')
	if result.compiled() and result.returncode() != 0
		error('Unable to determine buffer size to use for strerror_r(); NL_TEXTMAX is too large.')
	endif
endif

pthread = dependency('threads')
rt = cc.find_library('rt')

executable('which', 'which.c', dependencies: [pthread, rt])
